var WKTUtil = {
	regExes : {  
            'typeStr': /^\s*(\w+)\s*\s∗(.∗)\s∗\s*$/,  
            'spaces': /\s+/,  
            'parenComma': /\)\s*,\s*\(/,  
            'doubleParenComma': /\)\s*\)\s*,\s*\(\s*\(/,  // can't use {2} here  
            'trimParens': /^\s*?(.∗?)?\s*$/,
            	
        	'typeStr': /^\s*(\w+)\s*\(\s*(.*)\s*\)\s*$/,
			'spaces': /\s+|\+/, // Matches the '+' or the empty space
			'numeric': /-*\d+(\.*\d+)?/,
			'comma': /\s*,\s*/,
			'parenComma': /\)\s*,\s*\(/,
			'coord': /-*\d+\.*\d+ -*\d+\.*\d+/, // e.g. "24 -14"
			'doubleParenComma': /\)\s*\)\s*,\s*\(\s*\(/,
			'trimParens': /^\s*\(?(.*?)\)?\s*$/,
			'ogcTypes': /^(multi)?(point|line|polygon|box)?(string)?$/i, // Captures e.g. "Multi","Line","String"
			'crudeJson': /^{.*"(type|coordinates|geometries|features)":.*}$/ // Attempts to recognize JSON strings
        },
        read: function(wkt,spatialreference) {  
            var features, type, str;  
            wkt = wkt.replace(/[\n\r]/g, " ");  
            var matches = this.regExes.typeStr.exec(wkt);  
            if(matches) {  
                type = matches[1].toLowerCase();  
                str = matches[2];  
                if(this.parse[type]) {  
                    features = this.parse[type].apply(this, [str,spatialreference]);  
                    //console.log(features);  
                }  
            }      
            return features;  
        },  
        
        WktToPoint:function(wkt,spatialreference){  
            var pt = this.read(wkt);  
            var json = {  
                x:pt[0],  
                y:pt[1],  
                spatialReference: spatialreference  
            }  
            var point = new esri.geometry.Point(json);  
            return point;  
        } , 
        /** 
         *wkt转化成arcgis的Polyline对象 
         * @param wkt 
         * @returns {Polyline} 
         * @constructor 
         */  
        WktToPolyline:function(wkt, spatialreference){  
            var wktUtil = new WKTUtil();  
            var points = wktUtil.read(wkt);  
            var json = {  
                paths: [points],  
                spatialReference: spatialreference  
            }  
            var polyline = new esri.geometry.Polyline(json);  
            return polyline;  
        } , 
        /** 
         * wkt转化成arcgis的Polygon对象 
         * @param wkt 
         * @returns {Polygon} 
         * @constructor 
         */  
        WktToPolygon:function(wkt, spatialreference){  
            var wktUtil = new WKTUtil();  
            var points = wktUtil.read(wkt);  
            var json = {  
                    rings: [points],  
                    spatialReference: {"wkid":4326}  
            }  
            var polygon = new esri.geometry.Polygon(json);  
            return polygon;  
        },
        parse: {  
            /** 
             * Return point feature given a point WKT fragment. 
             * @param {String} str A WKT fragment representing the point 
             * @returns {OpenLayers.Feature.Vector} A point feature 
             * @private 
             */  
            'point': function(str,spatialreference) {  
                var coords = this.trim(str).split(this.regExes.spaces); 
                var json = {  
                        x:parseFloat(coords[0]),  
                        y:parseFloat(coords[1]),  
                        spatialReference: spatialreference  
                    }  
                var point = new esri.geometry.Point(json);  
                return point;  
            },  
              
            /** 
             * Return a linestring feature given a linestring WKT fragment. 
             * @param {String} str A WKT fragment representing the linestring 
             * @returns {OpenLayers.Feature.Vector} A linestring feature 
             * @private 
             */  
            'linestring': function(str,spatialreference) {  
                var points = this.trim(str).split(',');  
                  
                var components = [];  
                for(var i=0, len=points.length; i<len; ++i) {  
                    components.push(this.parse.point.apply(this, [points[i]]));  
                }  
                
                var paths = [];
                
                for(var i=0;i<components.length;i++){
                	var path = components[i];
                	paths.push([path.x,path.y]);
                }
                
                var json = {  
                        paths: [paths],  
                        spatialReference: spatialreference  
                    }  
                var polyline = new esri.geometry.Polyline(json);  
                return polyline;  
            },  
      
            /** 
             * Return a polygon feature given a polygon WKT fragment. 
             * @param {String} str A WKT fragment representing the polygon 
             * @returns {OpenLayers.Feature.Vector} A polygon feature 
             * @private 
             */  
            'polygon': function(str,spatialReference) {  
                var ring, linestring, linearring;  
                var rings = this.trim(str).split(this.regExes.parenComma);  
                  
                var components = [];  
                for(var i=0, len=rings.length; i<len; ++i) {  
                    ring = rings[i].replace(this.regExes.trimParens, '$1');  
                    linestring = this.parse.linestring.apply(this, [ring]);  
                    components.push(linestring);  
                }  
                var json = {  
                        rings: components[0].paths,  
                        spatialReference: spatialReference
                }  
                var polygon = new esri.geometry.Polygon(json);  
                return polygon;  
            }  
        },
        trim: function(str){  
            return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');  
        }
};